<?php
namespace store;

use Eelly\FastDFS\Client;
use utils\ConfigReader;
use store\FileBlockWriter;
use store\StorageType;
use utils\FileTool;
use utils\PathTool;
use utils\SysTool;

/*
	FastDFS块写入器
		
	更新记录：
	   2020-05-14 创建。
*/
class DfsBlockWriter extends FileBlockWriter
{
	function __construct() 
	{        
		$this->storage = StorageType::FastDFS;
	}	

	//创建文件,f_create.php调用
	function make($file)
	{
	    $data=null;
	    FileTool::writeAll($file->pathSvr, $data);
        $client = new Client(ConfigReader::fastDFSConfig());        
        $id = $client->uploadAppendFile($file->pathSvr,PathTool::getExtention($file->pathSvr));
		return $id;
	}
	
	/**
	 * 向文件中写入块数据
	 * @param long $blockOffset
	 * @param long $lenLoc
	 * @param string $pathSvr
	 * @param string $tmpFile
	 */
	function write($file,$data)
	{   
		$client = new Client(ConfigReader::fastDFSConfig());
		$client->appendFile($file->pathSvr,$data);
	}
	
	/**
	 * 向文件中写入块数据
	 * @param long $blockOffset
	 * @param long $lenLoc
	 * @param string $pathSvr
	 * @param string $tmpFile
	 */
	function writeData($blockOffset,$lenLoc,$pathSvr,$data)
	{
	    //写入数据
	    $sys = new SysTool();
	    $pathSvr = $sys->to_gbk($pathSvr);
	    $hfile = fopen($pathSvr,"r+b");
	    //定位到续传位置
	    fseek($hfile,intval($blockOffset),SEEK_SET);
	    fwrite($hfile,$data);
	    fclose($hfile);
	}
}
?>